package com.longtop.perform.kh.score.vo;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.longtop.framework.security.BaseUncheckedException;

/**
 * 
 * 保存评分数据模型-对应SQL_EXP函数的参数PARAM<br>
 * 备注：通用模型，具体PARAM对应的意思，请参考计分模型的JSP页面
 * 
 * @author 孙景弘
 */
public class ScoreParamVO {

	private String formula;// 评分模型对应的初始函数
	// 以下为函数带的参数名称：
	private Object PARAM1;
	private Object PARAM2;
	private Object PARAM3;
	private Object PARAM4;
	private Object PARAM5;
	private Object PARAM6;
	private Object PARAM7;
	private Object PARAM8;
	private Object PARAM9;
	private Object PARAM10;
	private Object PARAM11;

	public String getFormula() {
		return formula;
	}

	public void setFormula(String formula) {
		this.formula = formula;
	}

	/**
	 * 根据ScoreParamVO评分数据模型，构建其Formula(函数公式)<br>
	 * 评分数据模型,根据属性名称自动匹配替换函数参数。
	 * 
	 * @return 返回替换数据后的带参数函数公式
	 */
	public String buildFormulaFromBean() {
		String formulaStr = "";
		try {
			// bean对应的所有属性
			Field[] fields = this.getClass().getDeclaredFields();
			Field formulaField = null;
			for (int i = 0; i < fields.length; i++) {
				fields[i].setAccessible(true);
				String fieldName = fields[i].getName();
				if (fieldName.equals("formula")) {
					formulaField = fields[i];
					formulaStr = formulaField.get(this).toString();// 获取原评分模型的函数
				}
				// 替换评分模型函数参数:
				if (fieldName.indexOf("PARAM") != -1) {
					if (fields[i].get(this) != null) {
						String paramStr = fields[i].get(this).toString();
						formulaStr = formulaStr.replace(fieldName, paramStr);
					}
				}
			}
		} catch (Exception e) {
			throw new BaseUncheckedException("保存评分模型,转换评分公式异常。" + e.getMessage());
		}
		return formulaStr;
	}

	/**
	 * 根据Formula(函数公式)，构建其ScoreParamVO评分数据模型<br>
	 * Formula根据参数名称自动匹配替换评分数据模型。
	 * 
	 * @param formula
	 * 
	 * @return 返回替换数据后的评分数据模型
	 */
	public ScoreParamVO buildBeanFromFormula(String formula) {
		// 获取公式括号内的参数
		String subFormula = formula.substring(formula.indexOf("(") + 1, formula.length() - 1);
		String subStr = "";
		// 若参数存在'xxx'(即是集合)则先替换成null，并保存次集合参数为subStr
		if (subFormula.indexOf("'") != -1) {
			subStr = subFormula.substring(subFormula.indexOf("'"), subFormula.lastIndexOf("'") + 1);
			subFormula = subFormula.replace(subStr, "null");
		}
		// 组装参数到Map：
		String[] formulaArray = subFormula.split(",");
		Map<String, String> formulaMap = new HashMap<String, String>();
		int j = 1;
		for (int i = 0; i < formulaArray.length; i++) {
			String tmpStr = formulaArray[i].trim();
			try {
				Double.parseDouble(tmpStr);
			} catch (Exception e) {
				// 替换null参数为原先保存的subStr
				if (tmpStr.trim().equals("null")) {
					formulaMap.put("PARAM" + String.valueOf(j++), subStr);
				}
				continue;
			}
			formulaMap.put("PARAM" + String.valueOf(j++), tmpStr);
		}
		// for (Map.Entry<String, String> entry : formulaMap.entrySet()) {
		// System.out.println(entry.getKey() + " = " + entry.getValue() + ",");
		// }
		// 然后再从Map到bean对应的属性：
		BigDecimal tmp = null;
		try {
			// bean对应的所有属性
			Field[] fields = this.getClass().getDeclaredFields();
			for (int i = 0; i < fields.length; i++) {
				fields[i].setAccessible(true);
				String fieldName = fields[i].getName();
				// 替换评分模型函数参数:
				if (fieldName.indexOf("PARAM") != -1) {
					for (Map.Entry<String, String> entry : formulaMap.entrySet()) {
						if (fieldName.equals(entry.getKey())) {
							fields[i].set(this, entry.getValue());
							break;
						}
					}
				}
			}
		} catch (Exception e) {
			throw new BaseUncheckedException("替换评分函数公式到评分数据模型异常。" + e.getMessage());
		}
		return this;
	}

	public static void main(String[] args) {
		ScoreParamVO paramVO = new ScoreParamVO();
		paramVO = paramVO.buildBeanFromFormula("Scr_Grd_AddQty(1,2,3,-4,'5,6,7,8,9,10;' ,FS_Value)");
		// paramVO.buildBeanFromFormula("Scr_OVER_ADDRate(PT_Value ,Fs_Value ,60.2 ,2 ,3 ,4 ,5 ,-6.5)");
		paramVO = paramVO.buildBeanFromFormula("Scr_OVER_ADDRate(PT_Value ,Fs_Value ,1 ,4 ,5 ,6 ,2 ,3)");
		// System.out.println(paramVO.getPARAM1().getClass());
	}

	public Object getPARAM1() {
		if (PARAM1 != null) {
			BigDecimal decimal = new BigDecimal(PARAM1.toString());
			return decimal;
		} else {
			return null;
		}
	}

	public void setPARAM1(Object param1) {
		PARAM1 = param1;
	}

	public Object getPARAM2() {
		if (PARAM2 != null) {
			BigDecimal decimal = new BigDecimal(PARAM2.toString());
			return decimal;
		} else {
			return null;
		}
	}

	public void setPARAM2(Object param2) {
		PARAM2 = param2;
	}

	public Object getPARAM3() {
		if (PARAM3 != null) {
			BigDecimal decimal = new BigDecimal(PARAM3.toString());
			return decimal;
		} else {
			return null;
		}
	}

	public void setPARAM3(Object param3) {
		PARAM3 = param3;
	}

	public Object getPARAM4() {
		if (PARAM4 != null) {
			BigDecimal decimal = new BigDecimal(PARAM4.toString());
			return decimal;
		} else {
			return null;
		}
	}

	public void setPARAM4(Object param4) {
		PARAM4 = param4;
	}

	public Object getPARAM5() {
		if (PARAM5 != null) {
			BigDecimal decimal = new BigDecimal(PARAM5.toString());
			return decimal;
		} else {
			return null;
		}
	}

	public void setPARAM5(Object param5) {
		PARAM5 = param5;
	}

	public Object getPARAM6() {
		if (PARAM6 != null) {
			BigDecimal decimal = new BigDecimal(PARAM6.toString());
			return decimal;
		} else {
			return null;
		}
	}

	public void setPARAM6(Object param6) {
		PARAM6 = param6;
	}

	public Object getPARAM7() {
		if (PARAM7 != null) {
			BigDecimal decimal = new BigDecimal(PARAM7.toString());
			return decimal;
		} else {
			return null;
		}
	}

	public void setPARAM7(Object param7) {
		PARAM7 = param7;
	}

	public Object getPARAM8() {
		if (PARAM8 != null) {
			BigDecimal decimal = new BigDecimal(PARAM8.toString());
			return decimal;
		} else {
			return null;
		}
	}

	public void setPARAM8(Object param8) {
		PARAM8 = param8;
	}

	public Object getPARAM9() {
		if (PARAM9 != null) {
			BigDecimal decimal = new BigDecimal(PARAM9.toString());
			return decimal;
		} else {
			return null;
		}
	}

	public void setPARAM9(Object param9) {
		PARAM9 = param9;
	}

	public Object getPARAM10() {
		if (PARAM10 != null) {
			BigDecimal decimal = new BigDecimal(PARAM10.toString());
			return decimal;
		} else {
			return null;
		}
	}

	public void setPARAM10(Object param10) {
		PARAM10 = param10;
	}

	public Object getPARAM11() {
		if (PARAM11 != null) {
			BigDecimal decimal = new BigDecimal(PARAM11.toString());
			return decimal;
		} else {
			return null;
		}
	}

	public void setPARAM11(Object param11) {
		PARAM11 = param11;
	}
}
